<?php

/**
 * Created by PhpStorm.
 * User: chenzhitao
 * Date: 2018/5/22
 * Time: 上午11:24
 */
namespace app\index\model;
use think\Model;

class News extends Model
{
    protected $table = 't_news';
    public function tags()
    {
        return $this->belongsToMany('Tag','t_newstag',"tag_id","news_id");
    }
    public static function getSeoInfoByID($id){

        $news = self::find($id);
        $seo['title'] = $news['title'];
        $seo['desc'] = $news['desc'];
//        return json_encode($seo,JSON_UNESCAPED_UNICODE);
        return $seo;


    }
    public static function getRecList($num = 10){
        $data = self::where("flag","=",1)->order("created_at desc")
            ->field(['id',"title","desc","poster","key_words","created_at"])
            ->limit(0,$num)->select();
        if($data->isEmpty()){
            return[];
        }
        $news_list = [];
        foreach ($data as $item){
            $news_item["id"] = $item["id"];
            $news_item["title"] = $item["title"];
            $news_item["create_time"] = substr($item["created_at"],5,5);
            $news_item["poster"] =  config("app.img_prefix").$item['poster'];
            $tags = [];
            foreach ($item->tags as $tag){
                $tags[] = $tag["name"];
            }
            $tags = implode("-",$tags);
            $news_item["tags"] = $tags;
            array_push($news_list,$news_item);
        }
        return $news_list;
    }

    public static  function getTags($tags){
        $tag_list = "";
        if($tags){
            foreach ( $tags as $tag){
                $tag_list.=$tag['name'];
            }
        }
        return $tag_list;
    }
    public static function getHotList($num){
        $data = self::where("flag","=",1)->limit(0,$num)->order("view_num desc")
            ->field(['id',"title","desc","poster","key_words","view_num","created_at"])
            ->order("show_order","desc")->select();

        if($data->isEmpty()){
            return[];
        }
        $news_list = [];
        foreach ($data as $item){
            $news_item["id"] = $item["id"];
            $news_item["title"] = $item["title"];
            $news_item["desc"] = $item["desc"];
            $news_item["poster"] = config("app.img_prefix").$item["poster"];
            $news_item["view_num"] = $item["view_num"];
            $news_item["create_time"] = $item["created_at"];
            array_push($news_list,$news_item);
        }
        return $news_list;

    }
    public static function getNewsIndexList(){
        $data = self::where("flag","=",1)
            ->field(['id',"supcategory_id","subcategory_id","title","desc","poster","view_num","created_at"])
            ->limit(0,4)->order("view_num desc")
            ->order("show_order","desc")->select();
        if($data->isEmpty()){
            return[];
        }
        $category_list = [];
        $news_list = [];
        $category_id  = [];
        $sup_category_id =[];
        foreach ($data as $item){
            $news_item["id"] = $item["id"];
            $news_item["title"] = $item["title"];
            $news_item["desc"] = $item["desc"];
            $news_item["view_num"] = $item["view_num"];
            $news_item["poster"] = config('app.img_prefix').$item['poster'];
            $news_item["create_time"] = substr($item['created_at'],0,10);
            array_push($category_id,$item["subcategory_id"]);
            array_push($sup_category_id,$item["supcategory_id"]);
            array_push($news_list,$news_item);
        }
        $unique_category_id = array_unique ( $category_id );



        foreach ($unique_category_id as $id){
            $category_item["id"] = $id;
            $category_item["category_name"] = SubCategory::getCategoryNameById($id);
            array_push($category_list,$category_item);
        }
        $rand_index = array_rand($sup_category_id,1);
        $rand_supcategory_id = $sup_category_id[$rand_index];
        $topic_news_index = array_rand($news_list,1);
        $topic_news = $news_list[$topic_news_index];
        $footer_list["name"] = SupCategory::getCategoryNameById($rand_supcategory_id);
        $footer_list["onenews"] = self::getOneNewsBySupCategoryId($rand_supcategory_id,1);
        $footer_list["news_list"] = self::getOneNewsBySupCategoryId($rand_supcategory_id,2);
        $footer_list["sub_categorys"] = SubCategory::getChildCategorysByPid($rand_supcategory_id);
        $ret = [
            "hot_newsList"=>$news_list,
            "hot_categoryList"=>$category_list,
            "topic_news"=>$topic_news,
            "footer_list"=>$footer_list,
        ];
        return $ret;

    }
    public static function getOneNewsBySupCategoryId($supcateid,$num){
        $data = self::where("supcategory_id","=",$supcateid)
            ->where("flag","=",1)->order("view_num desc")
            ->field(['id',"title","desc","poster","created_at"])
            ->limit(0,$num)->order("show_order desc")->select();
        $news_list = [];
        foreach ($data as $item){
            $news_item = [];
            $news_item["id"] = $item["id"];
            $news_item["title"] = $item["title"];
            $news_item["desc"] = $item["desc"];
            $news_item["poster"] = config('app.img_prefix').$item['poster'];
            $news_item["create_time"] = substr($item['created_at'],0,10);

            array_push($news_list,$news_item);
        }
        return $news_list;
    }
    public static function getOne($id){
        $data = self::find($id);
        if($data == null){
            return [];
        }


        $news['id'] = $data['id'];
        $news['title'] = $data['title'];
        $news['view_num'] = $data['view_num'];
        $news['key_words'] = SubCategory::getCategoryNameById($data["subcategory_id"]);
//        $news['create_time'] = $data['created_at'];
        $news['create_time'] = str_replace('+00:00', 'T', gmdate('c', strtotime($data['created_at'])));
        $news['create_time'] =  substr($news['create_time'],0,strlen($news['create_time'])-1);
        $news['content'] = $data['content'];
        return $news;

    }
    public static function getLastId($id){
        $last_id = self::where('id', '<', $id)->max('id');
        return $last_id;
    }
    public static function getNextId($id){
        $next_id = self::where('id', '>', $id)->min('id');
        return $next_id;
    }
    public static function getList($page,$size = 10){
        $data = self::order("created_at desc")
            ->paginate($size, true, ['page' => $page]);
        $news_list = [];
        foreach ($data as $item){
            $news_item = [];
            $news_item["id"] = $item["id"];
            $news_item["title"] = $item["title"];
            $news_item["desc"] = $item["desc"];
            $news_item["key_words"] = $item["key_words"];
            $news_item["poster"] = config('app.img_prefix').$item['poster'];
            $news_item["create_time"] = $item['created_at'];
            array_push($news_list,$news_item);
        }
        return $news_list;
    }
    public static function getAll(){
        $data = self::where("flag","=",1)->select();
        return $data;
    }
    public static function getNewsBySubcategoryId($sub_category_id){
        $data = self::where("subcategory_id","=",$sub_category_id)->where("flag","=",1)
            ->field(['id',"title","desc","poster","key_words","view_num","created_at"])
            ->limit(1)->select();
        $news = [];
        foreach ($data as $item){
            $news["id"] = $item["id"];
            $news["title"] = $item["title"];
            $news["desc"] = $item["desc"];
            $news["key_words"] = $item["key_words"];
            $news["poster"] = config('app.img_prefix').$item['poster'];
            $news["create_time"] = $item['created_at'];
        }
        return $news;
    }
    public static function getRecestOne(){
        $data= self::where("flag","=",1)->order("created_at desc")->limit(1)->select();
        $news = [];
        foreach ($data as $item){
            $news["id"] = $item["id"];
            $news["title"] = $item["title"];
            $news["desc"] = $item["desc"];
            $news["key_words"] = $item["key_words"];
            $news["poster"] = config('app.img_prefix').$item['poster'];
            $news["create_time"] = $item['created_at'];
        }
        return $news;

    }




}